package ar.com.personalTravellers.daos.impl.propertyValue;

import com.angel.dao.generic.impl.GenericSpringHibernateDAO;
import com.angel.resourceBundle.daos.propertyValue.SimplePropertyValueDAO;
import com.angel.resourceBundle.propertyValue.PropertyValue;
import com.angel.resourceBundle.propertyValue.impl.SimplePropertyValue;
import com.angel.resourceBundle.transformers.PropertyValueToSimplePropertyValueTransformer;
import org.apache.commons.collections15.CollectionUtils;
import org.apache.commons.collections15.Transformer;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import java.io.Serializable;
import java.util.Collection;

/**
 *
 * @author William
 * @since 22/April/2009
 * 
 */
public class SimplePropertyValueSpringHibernateDAO extends GenericSpringHibernateDAO<SimplePropertyValue, Serializable> implements SimplePropertyValueDAO {

    public SimplePropertyValueSpringHibernateDAO() {
        super(SimplePropertyValue.class, Serializable.class);
    }

 	public void deleteUniqueByExtendedKey(String extendedKey) {
		super.delete((SimplePropertyValue) this.findUniqueByExtendedKey(extendedKey));
	}

	public PropertyValue findUniqueByExtendedKey(String extendedKey) {
		return super.findUnique("extendedKey", extendedKey);
	}

	public PropertyValue findUniqueOrNullByExtendedKey(String extendedKey) {
		return super.findUniqueOrNull("extendedKey", extendedKey);
	}

	@SuppressWarnings("unchecked")
	public Collection<PropertyValue> findAllByResourceBundleContextKey(String contextKey) {
        Collection<SimplePropertyValue> propertiesValues = super.findAllByCriteria(Restrictions.like("extendedKey", contextKey, MatchMode.START));
        return CollectionUtils.transformedCollection(propertiesValues, new Transformer<SimplePropertyValue, PropertyValue>(){
            public PropertyValue transform(SimplePropertyValue simplePropertyValue) {
                return simplePropertyValue;
            }
        });
    }

	public Long countAllWithExtendedKey(String extendedKey){
		return super.count("extendedKey", extendedKey);
	}

	@SuppressWarnings("unchecked")
	public Collection<PropertyValue> findAllByExtendedKey(String[] extendedKeys){
		Collection<SimplePropertyValue> propertiesValues = super.findAllByCriteria(Restrictions.in("extendedKey", extendedKeys));
        return CollectionUtils.transformedCollection(propertiesValues, new Transformer<SimplePropertyValue, PropertyValue>(){
            public PropertyValue transform(SimplePropertyValue simplePropertyValue) {
                return simplePropertyValue;
            }
        });
    }

    	public Collection<SimplePropertyValue> findAllBetweenCounter(Long minimumCounter, Long maximumCounter) {
		Collection<SimplePropertyValue> propertiesValues = super.findAllByCriteria(Restrictions.gt("counter", minimumCounter), Restrictions.lt("counter", maximumCounter));
		return CollectionUtils.transformedCollection(propertiesValues, new PropertyValueToSimplePropertyValueTransformer());
	}

	public Collection<SimplePropertyValue> findAllByCounter(Long counter) {
		Collection<SimplePropertyValue> propertiesValues = super.findAll("counter", counter);
		return CollectionUtils.transformedCollection(propertiesValues, new PropertyValueToSimplePropertyValueTransformer());
	}

	public SimplePropertyValue findUniqueByLabel(String label) {
		return super.findUnique("label", label);
	}
}